package Ss1;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Bmi extends Application {
    @Override
    public void start(Stage primaryStage)  {
        TextArea ta = new TextArea();
        Scene scene = new Scene(new ScrollPane(ta), 450, 200);
        primaryStage.setTitle("BmiComputer");
        primaryStage.setScene(scene);
        primaryStage.show();

        new Thread(() -> {
            try {
                ServerSocket serverSocket = new ServerSocket(8000);
                Platform.runLater(() ->
                        ta.appendText("时间： " + new Date() + '\n'));

                Socket socket = serverSocket.accept();

                DataInputStream inputFromClient = new DataInputStream(
                        socket.getInputStream());
                DataOutputStream outputToClient = new DataOutputStream(
                        socket.getOutputStream());

                while (true) {

                    double height = inputFromClient.readDouble();
                    double weight = inputFromClient.readDouble();

                    double Bim =weight/(height*height);
                    if (Bim<=18.5){
                        outputToClient.writeDouble(Bim);
                        outputToClient.writeUTF("瘦");
                    }else if (Bim<=24){
                        outputToClient.writeDouble(Bim);
                        outputToClient.writeUTF("标准");
                    }else {
                        outputToClient.writeDouble(Bim);
                        outputToClient.writeUTF("胖");
                    }



                    Platform.runLater(() -> {
                        ta.appendText("身高：" + height + '\n');
                        ta.appendText("体重: " + weight + '\n');

                    });
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }).start();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


